/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author manelv8
 */
public class ClientSocketTCP implements InterfaceClientSocket {

   Socket socket;

  
    
    public ClientSocketTCP(String host, int porta) throws UnknownHostException, IOException  {
         //super(host,porta);
         this.socket = new Socket(host,porta);
        
    }
    
    public ClientSocketTCP(Socket socket) throws UnknownHostException, IOException{
        this.socket = socket;
    }

    
      public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }
    
    @Override
    public void enviarMsg(String msg) {
        PrintStream wr = null;
        try {
            
            wr = new PrintStream(socket.getOutputStream());
            wr.println(msg);
        } catch (IOException ex) {
            Logger.getLogger(ClientSocketTCP.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
      //      wr.close();
        }
    }

    @Override
    public String receberMsg() {
        BufferedReader rd = null;
        String line ="";
        try {
            
            rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            line = rd.readLine();
        } catch (IOException ex) {
            Logger.getLogger(ClientSocketTCP.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            try {
//         //       rd.close();
//            } catch (IOException ex) {
//                Logger.getLogger(ClientSocketTCP.class.getName()).log(Level.SEVERE, null, ex);
//            }
        }
       
       return line;
    }

    @Override
    public String conectar() {
       
        return "";
    }

    @Override
    public void fechar() {
        try {
            socket.close();
        } catch (IOException ex) {
            Logger.getLogger(ClientSocketTCP.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
}
